package cn.sh.ideal.web.configuration.management.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import cn.sh.ideal.web.configuration.management.entity.AlarmRulesHostidDto;
import cn.sh.ideal.web.configuration.management.entity.ConfigQueryCondition;

/**
* @author: zhangyy
* 2018年5月31日
* monitorCenterWeb--告警个性化配置页面数据请求controller
*/
@Controller
@PropertySource(value = {"classpath:jdbc.properties"})
@RequestMapping(value = "/data")
public class WarningPersonalizedConfigDataController {
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}
	
	private RestTemplate rs = new RestTemplate();
	
	@Value("${server_address}")
	private String serverAddress;
	
	@RequestMapping(value = "/getWarningPersonalizedConfig", method = RequestMethod.POST)
	@ResponseBody
	public String getWarningPersonalizedConfig(@RequestBody ConfigQueryCondition parameter){
		String url = serverAddress+"/getWarningPersonalizedConfig?monitoringIndex=" + parameter.getMonitoringIndex()
				+"&page=" + parameter.getPage()
				+"&pageSize=" + parameter.getSize();
		
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);

		String result = exchange.getBody();
		
		return result;
	}
	
	@RequestMapping(value = "/getWarningPersonalizedMonitoringKpi", method = RequestMethod.POST)
	@ResponseBody
	public String getWarningPersonalizedMonitoringKpi(){
			String url = serverAddress+"/getWarningPersonalizedMonitoringKpi";
	
			HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
			headers.setContentType(type);
			HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
			
			ResponseEntity<String> exchange = rs.exchange(url,
					HttpMethod.GET, entity, String.class);
			
			String result = exchange.getBody();
			
			return result;
	}
	

	
	@RequestMapping(value = "/updateWarningPersonalizedConfig", method = RequestMethod.POST)
	@ResponseBody
	public String updateWarningPersonalizedConfig(@RequestBody String alarmConfigEntity){
		ArrayList<AlarmRulesHostidDto> parameterList = (ArrayList<AlarmRulesHostidDto>) JSON.parseObject(alarmConfigEntity, new TypeReference<ArrayList<AlarmRulesHostidDto>>(){});

		for(AlarmRulesHostidDto parameter : parameterList){
		String url = serverAddress+"/updateWarningPersonalizedConfig?desc=" + parameter.getDesc()
				+"&kpi=" + parameter.getKpi()
				+"&hostids=" + parameter.getHostids()
				+"&warningLv1ValueLow=" + parameter.getWarningLv1ValueLow()
				+"&warningLv1ValueHigh=" + parameter.getWarningLv1ValueHigh()
				+"&warningLv2ValueLow=" + parameter.getWarningLv2ValueLow()
				+"&warningLv2ValueHigh=" + parameter.getWarningLv2ValueHigh()
				+"&warningLv3ValueLow=" + parameter.getWarningLv3ValueLow()
				+"&warningLv3ValueHigh=" + parameter.getWarningLv3ValueHigh();
		
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);
		}
		//String result = exchange.getBody();
		
		return "";
	}
	
	
	@RequestMapping(value = "/deleteWarningPersonalizedConfig", method = RequestMethod.POST)
	@ResponseBody
	public String deleteWarningPersonalizedConfig(@RequestBody String parameter){
		ArrayList<String> parameterList = (ArrayList<String>) JSON.parseObject(parameter, new TypeReference<ArrayList<String>>(){});
		for(String param : parameterList){
			String url = serverAddress+"/deleteWarningPersonalizedConfig?desc=" + param;
			HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
	        headers.setContentType(type);
			HttpEntity<String> httpentity = new HttpEntity<>(new String(), headers);

			ResponseEntity<String> exchange = rs.exchange(url,
					HttpMethod.GET, httpentity, String.class);
		}
		
		return "";
	}
	
	@RequestMapping(value = "/insertWarningPersonalizedConfig", method = RequestMethod.POST)
	@ResponseBody
	public String insertWarningPersonalizedConfig(@RequestBody AlarmRulesHostidDto parameter){
		
		String url = serverAddress+"/insertWarningPersonalizedConfig?desc=" + parameter.getDesc()
				+"&kpi=" + parameter.getKpi()
				+"&warningLv1ValueLow=" + parameter.getWarningLv1ValueLow()
				+"&warningLv1ValueHigh=" + parameter.getWarningLv1ValueHigh()
				+"&warningLv2ValueLow=" + parameter.getWarningLv2ValueLow()
				+"&warningLv2ValueHigh=" + parameter.getWarningLv2ValueHigh()
				+"&warningLv3ValueLow=" + parameter.getWarningLv3ValueLow()
				+"&warningLv3ValueHigh=" + parameter.getWarningLv3ValueHigh()
				+"&hostids=" + parameter.getHostids();
		
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);
		
		//String result = exchange.getBody();
		
		return "";
	}
	@RequestMapping(value = "/updateWarningPersonalizedSelectIps", method = RequestMethod.POST)
	@ResponseBody
	public String updateWarningPersonalizedSelectIps(@RequestBody AlarmRulesHostidDto parameter){
	
		String url = serverAddress+"/updateWarningPersonalizedSelectIps?desc=" + parameter.getDesc()
				+"&kpi=" + parameter.getKpi()
				+"&hostids=" + parameter.getHostids()
				+"&deletedHostids=" + parameter.getDeletedHostids()
				+"&warningLv1ValueLow=" + parameter.getWarningLv1ValueLow()
				+"&warningLv1ValueHigh=" + parameter.getWarningLv1ValueHigh()
				+"&warningLv2ValueLow=" + parameter.getWarningLv2ValueLow()
				+"&warningLv2ValueHigh=" + parameter.getWarningLv2ValueHigh()
				+"&warningLv3ValueLow=" + parameter.getWarningLv3ValueLow()
				+"&warningLv3ValueHigh=" + parameter.getWarningLv3ValueHigh();
		
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url,
				HttpMethod.GET, entity, String.class);
		
		return "";
	}
}
